<?php

/**
 * @var $config
 * @var $key
 */


//修改数据库里更新的信息
include("../WeSql/widget.php");
include("../WeSql/config.php");
include("../WeSql/WeSql.php");

//创建更新
@$package = $_REQUEST['package'];
@$title = $_REQUEST['title'];
@$content = $_REQUEST['content'];
@$version = $_REQUEST['version'];
@$time = $_REQUEST['time'];

@$weSql = new WeSql($config,$key);

if ($package == "" or $title=="" or $content=="" or $version==""){
    $code = -1;
    $msg = "不能留空";
    $exception = "";
}else{
    if(is_numeric($version)){
        if (preg_match("/[a-zA-Z][.][0-9a-zA-Z]/",$package)){
            //检测包名是否合法
            if(!$weSql->isSelectField("updates",[
                "package" => $package
            ])){
                $code = -1;
                $msg = "包名不存在";
                $exception = "";
            }else{
                if($weSql->update("updates",[
                    "title" => $title,
                    "content" => $content,
                    "version" => $version,
                    "time" => $time
                ],"package='${package}'")){
                    $code = 1;
                    $msg = "修改成功";
                    $exception = "";
                }else{
                    $code = -1;
                    $msg = "修改失败";
                    $exception = "";
                }
            }

        }else{
            $code = -1;
            $msg = "包名不合法";
            $exception = "";
        }
    }else{
        $code = -1;
        $msg = "版本号错误";
        $exception = "";
    }
}

@$result = [
    "code" => $code,
    "msg" => $msg,
    "exception" => $exception
];
echo json_encode($result,JSON_UNESCAPED_UNICODE);